import { Component, OnInit } from '@angular/core';
import {FormControl,FormGroup} from '@angular/forms';

function validatePhone(c:FormControl){
  let phoneRegxp=new RegExp("1[0-9]{10}");
  return phoneRegxp.test(c.value)?null:{
    phone:{
      valid:false,
      errorMsg:'用户名必须是手机号'
  }
  }
}

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
  curcontact:Object={
    name1:'1',
    name2:'2'
  };
  form2:Object={
  };
   form3Obj:Object={
     name:''
   };  
  form4={
    phone:'122'    
  };
  loginControl:FormControl=new FormControl('');
  constructor() {
   }
   form5={};
  log(info:any){
    //console.log(this.curcontact);
    //console.log(this.form2);
    console.log(info)
  }
  doSubmit(formInfo:Object={}){
    console.log('打印提交信息');
    console.log(formInfo);
  }
  formValidate:FormGroup;
  ngOnInit() {
    this.formValidate=new FormGroup({  //表单实例对象
      'phone':new FormControl(this.form4.phone,validatePhone)//表单控件实例对象,第一个参数为控件返回值的初始值，第二个参数是校验配置方法，多个校验使用数组
    })
   
  }

}
